//
//  CNKIAnswerTableViewCell.m
//  CNKICommunity
//
//  Created by wei on 17/2/22.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "CNKIAnswerTableViewCell.h"
#import "UIImageView+MJWebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#import "UIButton+ButtonAnimal.h"
#import "UILabel+LableAnimal.h"


@interface CNKIAnswerTableViewCell()<CAAnimationDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *UserHead;
@property (weak, nonatomic) IBOutlet UILabel *UserName;
@property (weak, nonatomic) IBOutlet UILabel *HotPoint;
@property (weak, nonatomic) IBOutlet UILabel *AnswerTime;
@property (weak, nonatomic) IBOutlet UIButton *LoveYes;
@property (weak, nonatomic) IBOutlet UIButton *LoveNo;
@property (weak, nonatomic) IBOutlet UILabel *Myanswer;
@property (weak, nonatomic) IBOutlet UIView *LoveBottomView;
@property (weak, nonatomic) IBOutlet UIView *NoloveBottomview;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *TextHeight;
@property(nonatomic,strong)UIView *exampleimagePic;//举例视图
@property(nonatomic,strong)NSArray *urls;
@property(nonatomic,strong)UILabel *addOneLable;

@end


@implementation CNKIAnswerTableViewCell
- (IBAction)clickTheLove:(UIButton *)sender
{
    sender.enabled = NO;
    _AnswerModel.isLove = NO;
    [sender startLoveBttonAnimal:0.5];
    [self simulateNetwork];
    
}
- (IBAction)clickTheNolove:(UIButton *)sender
{
    [sender startLoveBttonAnimal:0.5];
}


-(void)simulateNetwork
{
    [self uploadNetwork];
}
-(void)uploadNetwork
{
    
    [[HttpsRefreshNetworking Networking] GET:[NSString stringWithFormat:@"http://192.168.22.246/WebApiTest/Message/GetMessage/10"] parameters:nil success:^(NSDictionary *allHeaders, id responseObject, id statusCode) {
        NSLog(@"%@",responseObject);
        NSLog(@"通知服务器成功");
        
        _addOneLable = [UILabel new];
        [_LoveBottomView addSubview:_addOneLable];
        [_addOneLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_LoveBottomView.mas_right).offset(-40);
            make.centerY.equalTo(_LoveBottomView.mas_centerY);
            make.height.equalTo(@20);
            make.width.equalTo(@30);
            
        }];
        [self setTheLabel:_addOneLable Text:@"+1"];
        
        //如果没成功 要改为yes 变按钮还能点击
        NSLog(@"----%@",_AnswerModel);
        _AnswerModel.isLove = NO;
    } failure:^(NSDictionary *allHeaders, NSError *error, id statusCode) {
        NSLog(@"点赞失败，请检查您的网络");
        _AnswerModel.isLove = YES;
        _LoveYes.enabled = YES;
    }];

    
    
    
    
}



-(void)setTheLabel:(UILabel*)label Text:(NSString*)text
{
    label.text = text;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor redColor];
    [label labelAnimal:1.5];
}
-(UIView *)exampleimagePic
{
    if (!_exampleimagePic)
    {
        _exampleimagePic = [UIView new];
        [self.contentView addSubview:_exampleimagePic];
        _exampleimagePic.hidden = YES;
        _urls = @[@"http://ww4.sinaimg.cn/thumbnail/7f8c1087gw1e9g06pc68ug20ag05y4qq.gif", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr0nly5j20pf0gygo6.jpg", @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1d0vyj20pf0gytcj.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg", @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr2n1jjj20gy0o9tcc.jpg", @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr39ht9j20gy0o6q74.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr3xvtlj20gy0obadv.jpg", @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr4nndfj20gy0o9q6i.jpg", @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr57tn9j20gy0obn0f.jpg"];
        UIImage *placeholder = [UIImage imageNamed:@"Welcome_3.0_5"];
        for (int i = 0; i < 3; i++)
        {
            UIImageView *IMAGE = [[UIImageView alloc]initWithFrame:CGRectMake(i * 80 + i * 2, 0, 80, 80)];
            [_exampleimagePic addSubview:IMAGE];
            IMAGE.tag = i;
            IMAGE.contentMode = UIViewContentModeScaleToFill;
            IMAGE.clipsToBounds = YES;
            IMAGE.userInteractionEnabled = YES;
            [IMAGE setImageURLStr:_urls[i] placeholder:placeholder];
            [IMAGE addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
        }
    }
    return _exampleimagePic;
}
-(void)tapImage:(UITapGestureRecognizer*)tap
{
    NSInteger count = _urls.count;
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count; i++)
    {
        NSString *url = [_urls[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc]init];
        photo.url = [NSURL URLWithString:url];
        if (i < 3)
        {
          photo.srcImageView = self.exampleimagePic.subviews[i];
        }
        else
        {
           photo.srcImageView = self.exampleimagePic.subviews[2];
        }
        [photos addObject:photo];
    }
    //显示相册
    MJPhotoBrowser *brower = [[MJPhotoBrowser alloc]init];
    brower.currentPhotoIndex = tap.view.tag;
    brower.photos = photos;
    [brower show];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    UILabel *lable = [UILabel new];
    [self.contentView addSubview:lable];
    lable.backgroundColor = [UIColor lightGrayColor];
    lable.font = [UIFont systemFontOfSize:14];
    lable.textColor = [UIColor blackColor];
    lable.numberOfLines = 0;

    _Myanswer = lable;

    
}

-(void)setAnswerModel:(CNKIAnswerModel *)AnswerModel
{
    _AnswerModel = AnswerModel;
    _LoveYes.enabled = AnswerModel.isLove;
    
    _Myanswer.text = AnswerModel.WordText;
    _Myanswer.frame = AnswerModel.TextFrame;
    
    //[_UserHead setHeader:AnswerModel.headUrl];
    _UserHead.contentMode = UIViewContentModeScaleAspectFill;
    _UserHead.clipsToBounds = YES;
    if ([AnswerModel.type isEqualToString:@"ispicture"])
    {
        //传数组图片
        self.exampleimagePic.hidden = NO;
        self.exampleimagePic.frame = AnswerModel.pictureFrame;
        
    }
    else
    {
        self.exampleimagePic.hidden = YES;
    }
    
    
    
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
